package action.contract;

import java.util.List;

import core.action.BaseAction;
import core.global.Global;
import exception.Exceptions;
import service.contract.ContractService;
import service.contract.ShowContractService;
import util.Sessions;
import util.Time;
import model.company.Companybasic;
import model.contract.Contractensure;
import model.credit.Applycustomrelation;
import model.personal.Personalbasic;

public class ContractEnsureNewAction extends BaseAction{
	private Contractensure contractensure;
	private String contractY;
	private String contractnumber;
	private String customcode;
	private ContractService contractService;
	private ShowContractService showContractService;
	private Integer relationid;
	public String execute() throws Exceptions{
		Integer creditid=(Integer)Sessions.get("creditid");
		List<Contractensure> idList=showContractService.get_htEnsure();
		String contractloanid=showContractService.get_ht("applyid", creditid).getContractId();
		//String contractloanid=(String)Sessions.get("contractloanid");
		Integer customid=contractService.getapplyrelation("id",relationid).getRelationpeopleid();
		contractensure.setContractloanid(contractloanid);
		String ensureid="";
		if(contractensure.getEnsuretype().equals("1"))
		{
			ensureid=contractY+"年个保字第"+contractnumber+"号";
			Personalbasic personal=contractService.get_personalbasic(customid);
			contractensure.setCustomensureid(personal.getId());
			contractensure.setCustomensuretype("1");	
		}else if(contractensure.getEnsuretype().equals("2")){
			ensureid=contractY+"年企保字第"+contractnumber+"号";
			Companybasic company=contractService.get_companybasic(customid);
			contractensure.setCustomensureid(company.getId());
			contractensure.setCustomensuretype("2");
		}
		if(idList != null){
		for(Contractensure e:idList){
			if(e.getCustomensureid().equals(ensureid)){
				MessageJump("重复的合同编号","BACK");
				return "message";
			}
		}
		}
		contractensure.setEnsureid(ensureid);
		contractensure.setClerkid(Global.userid().intValue());
		contractensure.setCreatetime(Time.CurrentTimestamp());
		contractensure.setState("10");
		if(contractensure.getContractensuredate().after(contractensure.getCreatetime())){
			MessageJump("合同签订日期填写有误！","BACK");
			return "message";
		}
		contractService.save_in_contractensure(contractensure);
		Applycustomrelation applyrelation=contractService.getapplyrelation("id",(Integer)Sessions.get("applyensurerelationid"));
		applyrelation.setState("0");
		contractService.save_in_applycustomrelation(applyrelation);
		if(contractService.get_countrelation(creditid, 3)==0){
			MessageJump("操作已完成","showContract!showcheck");
		}else{
			MessageJump("还存在保证合同未生成","contractEnsureListAction!getList");
		}
		return "message";
	}

	public Contractensure getContractensure() {
		return contractensure;
	}

	public void setContractensure(Contractensure contractensure) {
		this.contractensure = contractensure;
	}

	public String getContractY() {
		return contractY;
	}

	public void setContractY(String contractY) {
		this.contractY = contractY;
	}

	public String getContractnumber() {
		return contractnumber;
	}

	public void setContractnumber(String contractnumber) {
		this.contractnumber = contractnumber;
	}

	public String getCustomcode() {
		return customcode;
	}

	public void setCustomcode(String customcode) {
		this.customcode = customcode;
	}

	public ContractService getContractService() {
		return contractService;
	}

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}

	public ShowContractService getShowContractService() {
		return showContractService;
	}

	public void setShowContractService(ShowContractService showContractService) {
		this.showContractService = showContractService;
	}

	public Integer getRelationid() {
		return relationid;
	}

	public void setRelationid(Integer relationid) {
		this.relationid = relationid;
	}

}
